$(function(){
	$("#deviceType").change(function(){
		deviceTypeChange(this,"dictionaryType",true);
	});
	$("#deviceType_add").change(function(){
		deviceTypeChange(this,"dictionaryType_add");
	});
	goToPage(1);
});

function deviceTypeChange(obj,selectId,hasDefault){
	$.ajax({
		url : contextPath+"admin/dictionary/selectTypeByParent",
		data : {
			pid:$(obj).val()
		},
		type : 'POST',
		async:false,
		success:function(data){
			var options = '';
			if(hasDefault){
				options = '<option value="0">字典类型</option>';
			}
			for(var i = 0; i < data.length; i++){
				options += '<option value="'+data[i].dictionaryTypeId+'">'+data[i].typeName+"</option>";
			}
			$("#"+selectId).html(options);
		}
				
	});
}

function getDeviceName(dictionaryType){
	var special = {
		"200":"118",
		"201":"118",
		"202":"118",
		"203":"118",
		"204":"118",
		"205":"118",
		"206":"118",
		"207":"118",
		"208":"118",
		"209":"118",
		"210":"118"
	}
	
	if(dictionaryType!=null && dictionaryType.length > 3){
		var device = dictionaryType.substring(0,dictionaryType.length-3);
		if(special[device]){
			device = special[device];
		}
		return val(deviceType[device]);
	}
	return "";
}

function goToPage(pageNo){
	$.ajax({
		url : contextPath+ 'admin/dictionary/list',
		data : {
			pageSize : 10,
			pageNo : pageNo,
			pid:$("#deviceType").val(),
			deviceType:$("#dictionaryType").val(),
			status:$("#status").val()
		},
		type : 'POST',
		success : function(list) {
			$("#content").empty();
			$(".page").empty();
			if(list!=null && list.totalRecord>0){
				var obj = list.records;
				for(var i=0;i<obj.length;i++){
					var div= '<div class="pwsj_right_czrz_cont"><ul class="czrz_cont_ul">';
					div += '<li style="width:5%;">'+(i+1)+'</li>';
					div += '<li style="width:25%;">'+getDeviceName(obj[i].dictionaryTypeId+"")+'</a></li>';
					div += '<li style="width:25%;">'+dictionaryType[obj[i].dictionaryTypeId]+'</a></li>';
					div += '<li style="width:25%;">'+obj[i].dictionaryValue+'</li>';
					/*div += '<li style="width:10%;">'+(obj[i].status==1?'审核通过':'待审核')+'</li>';*/
					div += '<li style="width:19%;border-right:none;">'
					if(obj[i].status!=1){
						div += '<a class="detail_a" href="javascript:showApprove('+obj[i].dictionaryId+')">审核</a> ';
					}
					div	+='<a class="detail_a" href="javascript:showUpdate('+obj[i].dictionaryId+')">修改</a> '
					div	+='<a class="detail_a" href="javascript:deleteDictionary('+obj[i].dictionaryId+')"> 删除</a> </li>';
					div +=	'</li>';
					div += '</ul></div>';
					$("#content").append(div);
				}
				pageList(list);
			}
		}
	});
}

function showApprove(id){
	$(".add_members_box").hide();
	$("#approveBox").show();
	$.get(
		contextPath+ 'admin/dictionary/load',
		{
			id:id
		},
		function(data){
			if(!data){
				alert("查询失败");
				return;
			}
			$("#dictionaryId_approve").val(data.dictionaryId);
			$("#dictionaryTypeName_approve").html(data.dictionaryTypeName);
			$("#dictionaryValue_approve").html(data.dictionaryValue);
			$("#createor_approve").html(data.createor);
		}
	);
}

function approve(status){
	var msg = "确认通过吗？";
	if(status != 1){
		msg = "拒绝将会删除此数据，确认拒绝吗？";
	}
	pwtzconfirm(msg,function(){
		$.post(
			contextPath+ 'admin/dictionary/approve',
			{
				id:$("#dictionaryId_approve").val(),
				status : status
			},
			function(data){
				alert(data.msgDesc);
				if(data.opResult == '0'){
					$(".add_members_box").hide();
					goToPage(1);
				}	
			}
		);
	});
	
}

function addShow(){
	$(".add_members_box").hide();
	$("#addBox").show();
	//默认选中查询栏中的值
	$("#deviceType_add").val($("#deviceType").val());
	deviceTypeChange($("#deviceType_add"),"dictionaryType_add");
	$("#dictionaryType_add").val($("#dictionaryType").val());
}

function addDictionary(){
	$.post(
		contextPath+ 'admin/dictionary/add',
		$("#addForm").serialize(),
		function(data){
			alert(data.msgDesc);
			if(data.opResult == '0'){
				$(".add_members_box").hide();
				//默认查询添加类型的数据
				$("#deviceType").val($("#deviceType_add").val());
				deviceTypeChange($("#deviceType"),"dictionaryType",true);
				$("#dictionaryType").val($("#dictionaryType_add").val());
				$("#status").val("");
				
				goToPage(1);
			}
		}
	);
}
function showUpdate(id){
	$(".add_members_box").hide();
	$("#updateBox").show();
	$.get(
		contextPath+ 'admin/dictionary/load',
		{
			id:id
		},
		function(data){
			if(!data){
				alert("查询失败");
				return;
			}
			autoWrite(data,"#updateForm");
		}
	);
	
}
function updateDictionary(){
	$.post(
		contextPath+ 'admin/dictionary/update',
		$("#updateForm").serialize(),
		function(data){
			alert(data.msgDesc);
			if(data.opResult == '0'){
				$(".add_members_box").hide();
				goToPage(1);
			}
		}
	);
}
function deleteDictionary(id){
	pwtzconfirm("确定要删除吗?",function(){
		$.post(
			contextPath+ 'admin/dictionary/delete',
			{id:id},
			function(data){
				alert(data.msgDesc);
				if(data.opResult == '0'){
					$(".add_members_box").hide();
					goToPage(1);
				}
			}
		);
	});
}